package com.topsec.erp.util;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Sort;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by huangyongqiang
 * Created with IntelliJ IDEA
 * JDK：1.7.0_71
 * Date: 2017/7/3
 * 用于在各个 Controller 中的 pageList 中的时间排序
 */
public class TimeSortUtil {
    public static List convertTimer(List list, int dates, String flag) {
        List arrayList = new ArrayList();
        try {
            List times = getTimes(Calendar.DAY_OF_MONTH, dates);
            HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
            for (Object o : list) {
                Map sts = (Map) o;
                String s = sts.get("time").toString();
                String s1 = sts.get("count").toString();
                stringStringHashMap.put(s, s1);
            }
            if ("five".equals(flag)) {
                int i = 0;
                int sum = 0;
                for (Object time : times) {
                    i++;
                    if (stringStringHashMap.containsKey(time.toString()) && stringStringHashMap.get(time.toString()) != null) {
                        sum = sum + Integer.parseInt(stringStringHashMap.get(time.toString()));
                    }
                    if (i % 7 == 0) {
                        arrayList.add(sum);
                        sum = 0;
                    }
                }
            } else {
                for (Object time : times) {
                    Map map = new HashMap();
                    map.put("name", time);
                    if (stringStringHashMap.containsKey(time.toString())) {
                        map.put("value", stringStringHashMap.get(time.toString()));
                    } else {
                        map.put("value", "0");
                    }

                    arrayList.add(map);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static synchronized List getTimes(int type, int dates) throws Exception {
        String defPattern = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(defPattern);
        List listKey = new ArrayList();

        Calendar startCalendar = Calendar.getInstance();
        Date end = new Date();
        startCalendar.add(type, -dates);
        while (startCalendar.getTime().getTime() <= end.getTime()) {
            listKey.add(sdf.format(startCalendar.getTime()));
            startCalendar.add(type, 1);
        }

        return listKey;
    }

    public static String returnSort(String unjsonStr) {
        Map unjson = FastJsonUtil.toList(unjsonStr, Map.class, "sort");
        Map sortcloumn = FastJsonUtil.toList(unjsonStr, Map.class, "sortcloumn");
        Map sortorder = FastJsonUtil.toList(unjsonStr, Map.class, "sortorder");
        String returnStr = "commitTime";
        if (unjson != null && unjson.size() > 0) {
            String id = sortcloumn.get("iSortCol_0") == null ? "" : sortcloumn.get("iSortCol_0").toString();
            String toSort = sortorder.get("sSortDir_0") == null ? "" : sortorder.get("sSortDir_0").toString();
            if (StringUtils.isNotEmpty(id) && !"0".equals(id) && StringUtils.isNotEmpty(toSort)) {
                String tempPro = unjson.get(id) == null ? "creationTime" : unjson.get(id).toString();
                returnStr = tempPro;
            }
        }

        return returnStr;
    }

    /**
     * 页面排序规则
     *
     * @param unjsonStr 页面传递的datatable的参数
     * @param defaut    排序列
     */
    public static Sort returnSort(String unjsonStr, String defaut) {
        Map unjson = FastJsonUtil.toList(unjsonStr, Map.class, "sort");            // 页面表格中可排序的字段
        Map sortcloumn = FastJsonUtil.toList(unjsonStr, Map.class, "sortcloumn");  // 排序列标识 0:默认排序
        Map sortorder = FastJsonUtil.toList(unjsonStr, Map.class, "sortorder");    // 排序规则


        // table默认排序关键词
        String returnStr = "commitTime";
        if (StringUtils.isNotEmpty(defaut)) {
            returnStr = defaut;
        }
        String toSort = "desc";
        if (unjson != null && unjson.size() > 0) {
            String id = sortcloumn.get("iSortCol_0") == null ? "" : sortcloumn.get("iSortCol_0").toString();
            toSort = sortorder.get("sSortDir_0") == null ? "" : sortorder.get("sSortDir_0").toString();
            // 当前为默认排序
            if ("0".equals(id)) {
                toSort = "desc";
            }

            // 当前为指定列排序
            if (StringUtils.isNotEmpty(id) && !"0".equals(id) && StringUtils.isNotEmpty(toSort)) {
                String tempPro = unjson.get(id) == null ? "creationTime" : unjson.get(id).toString();
                if("toolName".equals(tempPro)){
                    tempPro = "toolId";
                }
                returnStr = tempPro;
            }
        }
        Sort sort = null;
        if ("asc".equals(toSort)) {
            sort = new Sort(Sort.Direction.ASC, returnStr);
        } else {
            sort = new Sort(Sort.Direction.DESC, returnStr);
        }
        return sort;
    }


    /**
     * 页面排序规则
     *
     * @param unjsonStr 页面传递的datatable的参数
     * @param defaut    排序列
     */
    public static Sort returnSort(String unjsonStr, String... defaut) {
        Map unjson = FastJsonUtil.toList(unjsonStr, Map.class, "sort");            // 页面表格中可排序的字段
        Map sortcloumn = FastJsonUtil.toList(unjsonStr, Map.class, "sortcloumn");  // 排序列标识 0:默认排序
        Map sortorder = FastJsonUtil.toList(unjsonStr, Map.class, "sortorder");    // 排序规则


        // table默认排序关键词
        String[] returnStr = {"commitTime"};
        if (defaut.length > 0) {
            returnStr = defaut;
        }

        String toSort = "desc";
        if (unjson != null && unjson.size() > 0) {
            String id = sortcloumn.get("iSortCol_0") == null ? "" : sortcloumn.get("iSortCol_0").toString();
            toSort = sortorder.get("sSortDir_0") == null ? "" : sortorder.get("sSortDir_0").toString();
            // 当前为默认排序
            if ("0".equals(id)) {
                toSort = "desc";
            }

            // 当前为指定列排序
            if (StringUtils.isNotEmpty(id) && !"0".equals(id) && StringUtils.isNotEmpty(toSort)) {
                String[] tempPro;
                if (unjson.get(id) == null) {
                    tempPro = returnStr;
                } else {
                    List<String> li = new ArrayList<>();
                    for (String s : returnStr) {
                        if (!"commitTime".equals(s)) {
                            li.add(s);
                        }
                    }

                    String[] strings = new String[li.size() + 1];
                    for (int i = 0; i < li.size(); i++) {
                        strings[i] = li.get(i);
                    }
                    strings[strings.length - 1] = unjson.get(id).toString();

                    tempPro = strings;
                }
                returnStr = tempPro;
            }
        }
        Sort sort = null;
        if ("asc".equals(toSort)) {
            sort = new Sort(Sort.Direction.ASC, returnStr);
        } else {
            sort = new Sort(Sort.Direction.DESC, returnStr);
        }

        return sort;
    }

//    public static SortVO returnSortVO(String unjsonStr, String defaut) {
//        Map unjson = FastJsonUtil.toList(unjsonStr, Map.class, "sort");
//        Map sortcloumn = FastJsonUtil.toList(unjsonStr, Map.class, "sortcloumn");
//        Map sortorder = FastJsonUtil.toList(unjsonStr, Map.class, "sortorder");
//
//
//        // table默认排序关键词
//        String returnStr = "commit_time";
//        if (StringUtils.isNotEmpty(defaut)) {
//            returnStr = defaut;
//        }
//        String toSort = "desc";
//        if (unjson != null && unjson.size() > 0) {
//            String id = sortcloumn.get("iSortCol_0") == null ? "" : sortcloumn.get("iSortCol_0").toString();
//            toSort = sortorder.get("sSortDir_0") == null ? "" : sortorder.get("sSortDir_0").toString();
//            if ("0".equals(id)) {
//                toSort = "desc";
//            }
//            if (StringUtils.isNotEmpty(id) && !"0".equals(id) && StringUtils.isNotEmpty(toSort)) {
//                String tempPro = unjson.get(id) == null ? "creationTime" : unjson.get(id).toString();
//                returnStr = tempPro;
//            }
//        }
//        SortVO sort = new SortVO();
//        if ("asc".equals(toSort)) {
//            sort.setSort("asc");
//            sort.setSortColumn(returnStr);
//        } else {
//            sort.setSort("desc");
//            sort.setSortColumn(returnStr);
//        }
//        return sort;
//    }
}
